package by.epam.xslttask.controller;

import by.epam.xslttask.command.*;
import by.epam.xslttask.constants.ProductConstants;
import by.epam.xslttask.dto.Product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Darya_Hrishanovich
 * Date: 7/6/12
 * Time: 3:52 AM
 * To change this template use File | Settings | File Templates.
 */
public class ControllerHelper {
    private static final String COMMAND = "command";
    private static final String PAGE = "page";

    public Command identifyCommand(HttpServletRequest request, HttpServletResponse response,
                                   String relPath) throws IOException {
        Command command = null;
        CommandType commandType = CommandType.valueOf(request.getParameter(COMMAND).toUpperCase());
        PrintWriter out = response.getWriter();

        switch (commandType) {
            case SHOW:
                String elementType = request.getParameter(ProductConstants.TYPE);
                Map params = request.getParameterMap();
                command = new ShowXSLCommand(relPath,elementType, params);
                command.setOut(out);
                break;
            case SAVE:
                String category = request.getParameter(ProductConstants.CATEGORY);
                String subcategory = request.getParameter(ProductConstants.SUBCATEGORY);
                String name = request.getParameter(ProductConstants.NAME);
                String producer = request.getParameter(ProductConstants.PRODUCER);
                String model = request.getParameter(ProductConstants.MODEL);
                String date = request.getParameter(ProductConstants.DATE);
                String color = request.getParameter(ProductConstants.COLOR);
                boolean notInStock = (request.getParameter(ProductConstants.NOT_IN_STOCK) != null);
                String price = request.getParameter(ProductConstants.PRICE);
                Product product = new Product();
                product.setName(name);
                product.setProducer(producer);
                product.setModel(model);
                product.setDate(date);
                product.setColor(color);
                product.setNotInStock(notInStock);
                product.setPrice(price);
                product.setSubcategory(subcategory);
                product.setCategory(category);
                command = new SaveCommand(relPath, product);
                command.setOut(out);
                break;
            case BACK:
                String backPage = request.getParameter(PAGE);         // page where to return
                params = request.getParameterMap();
                command = new BackCommand(relPath, backPage, params);
                command.setOut(out);
                break;
        }
        return command;
    }
}
